package ru.slovoslovo.cropmotion.ui.filetree;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

public class FileViewerSorter extends ViewerSorter {
    
    public int compare(Viewer viewer, Object o1, Object o2) {
        int result = 0;
        
        if (o1 instanceof FileTreeNode && o2 instanceof FileTreeNode){
            FileTreeNode node1 = (FileTreeNode) o1; 
            FileTreeNode node2 = (FileTreeNode) o2;
            if (node1.getFile().isDirectory() && !node2.getFile().isDirectory()){
                result = -1;
            }else if (!node1.getFile().isDirectory() && node2.getFile().isDirectory()){
                result = 1;
            }else{
                result = ((FileTreeNode)o1).getFile().getName().compareTo(((FileTreeNode)o1).getFile().getName()); 
            }
        }
            
        return result;
    }
    
}